﻿using IdentityServer4.Test;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class AccountController : ControllerBase
    {
        private readonly ILogger<AccountController> logger;
        private readonly TestUserStore userStore;

        public AccountController(ILogger<AccountController> logger, TestUserStore userStore)
        {
            this.logger = logger;
            this.userStore = userStore;
        }

        [HttpGet]
        public ActionResult LogIn([FromQueryAttribute] string userName, [FromQueryAttribute] string password)
        {
            TestUser user = userStore.FindByUsername(userName);
            if (user == null)
            {
                return NotFound("没有找到user信息！！");
            }
            if (userStore.ValidateCredentials(userName, password))
            {
                return BadRequest("密码错误！");
            }
            Microsoft.AspNetCore.Authentication.AuthenticationProperties properties = new Microsoft.AspNetCore.Authentication.AuthenticationProperties
            {
                IsPersistent = true,
                ExpiresUtc = DateTime.UtcNow.AddSeconds(1000),
            };
             //Microsoft.AspNetCore.Http.AuthenticationManagerExtensions.SignInAsync(HttpContext,user,  properties).Wait();
            return Ok(user);
        }
    }
}
